﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TouchDriver;
using TouchDriver.Data;
using System.IO;

namespace CSWhiteBoard
{
    public partial class WhiteBoard : Form
    {
        private bool mouseDown = false;
        private Point startLocation;
        private int imageWidth;
        private int imageHeight;
        private TouchDriver.TouchDriver driver = new TouchDriver.TouchDriver();

        public WhiteBoard()
        {
            InitializeComponent();

            Init();
        }

        private void Init()
        {
            pictureBox.MouseDown += new MouseEventHandler(pictureBox_MouseDown);
            pictureBox.MouseMove += new MouseEventHandler(pictureBox_MouseMove);
            pictureBox.MouseUp += new MouseEventHandler(pictureBox_MouseUp);
            pictureBox.MouseWheel += new MouseEventHandler(pictureBox_MouseWheel);


            listImages.ItemDrag += new ItemDragEventHandler(listImages_ItemDrag);
            listImages.DoubleClick += new EventHandler(listImages_DoubleClick);
            listImages.LargeImageList = smallImageList;
            listImages.View = View.LargeIcon;

            
            driver.MouseClickEvent += new TouchDriver.TouchDriver.MouseClickHandler(driver_MouseClickEvent);
            driver.SingleMoveEvent += new TouchDriver.TouchDriver.SingleMoveHandler(driver_SingleMoveEvent);
            driver.DoubleMoveEvent += new TouchDriver.TouchDriver.DoubleMoveHandler(driver_DoubleMoveEvent);

            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);

            backgroundWorker.RunWorkerAsync();
        }

        void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //用户退出校准
            Application.Exit();
        }

        void pictureBox_MouseWheel(object sender, MouseEventArgs e)
        {
            ZoomImage(e.Delta, e.Delta);
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            driver.Start();
        }

        void driver_DoubleMoveEvent(Record begin, Record end)
        {
            int beginWidth = abs(begin.loc1.x - begin.loc2.x);
            int beginHeight = abs(begin.loc1.y - begin.loc2.y);

            int endWidth = abs(end.loc1.x - end.loc2.x);
            int endHeight = abs(end.loc1.y - end.loc2.y);

            ZoomImage(endWidth - beginWidth, endHeight - beginHeight);
        }

        void driver_SingleMoveEvent(Location begin, Location end)
        {
            //TODO
            //MoveImage();
        }

        void driver_MouseClickEvent(Location loc)
        {
            Mouse.DoubleClick(loc.x, loc.y);
        }

        void listImages_DoubleClick(object sender, EventArgs e)
        {
            if(listImages.SelectedItems != null && listImages.SelectedItems.Count == 1)
            {
             
                pictureBox.Image = largeImageList.Images[listImages.SelectedItems[0].ImageIndex];
            }
        }

        void listImages_ItemDrag(object sender, ItemDragEventArgs e)
        {
            listImages.DoDragDrop(e.Item, DragDropEffects.Move);
        }


        void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;

            MoveImage(e.X - startLocation.X, e.Y - startLocation.Y);   

            imageWidth = pictureBox.Width;
            imageHeight = pictureBox.Height;
        }

        void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if(mouseDown)
            {
                MoveImage(e.X - startLocation.X, e.Y - startLocation.Y);   
            }
        }

        void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox.Focus();

            mouseDown = true;
            startLocation = e.Location;
        }

        void singleFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image| *.jpg;*.png;*.gif";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                addImageFromFile(dialog.FileName);
            }
        }

        void directoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                addImageFromDirecotry(dialog.SelectedPath);
            }
        }

        void cleanUpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listImages.Clear();
            smallImageList.Images.Clear();
            largeImageList.Images.Clear();
            pictureBox.Image = null;
        }

        private void addImageFromFile(string path)
        {
            smallImageList.Images.Add(new System.Drawing.Bitmap(path));
            largeImageList.Images.Add(new System.Drawing.Bitmap(path));

            ListViewItem item = new ListViewItem();
            item.ImageIndex = smallImageList.Images.Count - 1;

            listImages.Items.Add(item);
        }

        private void addImageFromDirecotry(string path)
        {
            string[] jpgfiles = Directory.GetFiles(path, "*.jpg");
            string[] pngfiles = Directory.GetFiles(path, "*.png");
            string[] giffiles = Directory.GetFiles(path, "*.gif");
            
            if(jpgfiles != null)
            {
                for (int i = 0; i < jpgfiles.Length; ++i )
                {
                    addImageFromFile(jpgfiles[i]);
                }
            }

            if (pngfiles != null)
            {
                for (int i = 0; i < pngfiles.Length; ++i)
                {
                    addImageFromFile(pngfiles[i]);
                }
            }

            if (giffiles != null)
            {
                for (int i = 0; i < giffiles.Length; ++i)
                {
                    addImageFromFile(giffiles[i]);
                }
            }
        }

        private void ZoomImage(int deltaW, int deltaH)
        {
            if((pictureBox.Width <= 100 && deltaW <= 0) ||
                (pictureBox.Height <= 100 && deltaH <= 0))
            {
                return;
            }

            pictureBox.Invalidate();

            pictureBox.Left -= deltaW / 2;
            pictureBox.Width += deltaW;

            pictureBox.Top -= deltaH / 2;
            pictureBox.Height += deltaH;

            pictureBox.Update();
        }

        private void MoveImage(int deltaX, int deltaY)
        {
            int x = pictureBox.Location.X + deltaX;
            int y = pictureBox.Location.Y + deltaY;

            pictureBox.Invalidate();
            pictureBox.Update();

            pictureBox.Location = new Point(x, y);
        }

        private int abs(int a)
        {
            return a < 0 ? -a : a;
        }
    }
}
